package com.kuang.demo.service;

import com.kuang.demo.bo.zzy.LogBo.ListPageLimit2;
import com.kuang.demo.bo.zzy.Role.*;
import com.kuang.demo.bo.zzy.TotalItem;
import com.kuang.demo.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<ValueLabel> selectAllRole() {
        List<ValueLabel> labels = roleMapper.selectAllRole();

        return labels;
    }

    @Override
    public TotalItem selectAllRoleByLimit(ListPageLimit2 listPageLimit) {

        int total = roleMapper.selectAllRoles(listPageLimit.getName());
        listPageLimit.setPage((listPageLimit.getPage()-1) * listPageLimit.getLimit());
        List<IdNaEnaAddUpDe> list = roleMapper.selectRoleByLimit(listPageLimit);


        TotalItem totalItem = new TotalItem();
        totalItem.setItems(list);
        totalItem.setTotal(total);


        return totalItem;
    }

    @Override
    public CreateRole createRole(NameDesc nameDesc) {
        //首先插入数据
        roleMapper.createRole(nameDesc);

        //再查询数据
        CreateRole createRole = roleMapper.selectCreateRole(nameDesc.getName());

        return createRole;
    }

    @Override
    public int selectRoleName(String name) {
        int i = roleMapper.selectRoleName(name);
        return i;
    }

    @Override
    public void updateRole(IdNaEnaAddUpDe idNaEnaAddUpDe) {
        roleMapper.updateRole(idNaEnaAddUpDe);
    }

    @Override
    public void deleteRole(IdNaEnaAddUpDe idNaEnaAddUpDe) {
        roleMapper.deleteRole(idNaEnaAddUpDe.getId());
    }

    @Override
    public ReturnPermission selectAllPermission(int roleId) {
        //先查全部列表
       List<MaxPermission> list = roleMapper.selectAllPermission();
        ReturnPermission returnPermission = new ReturnPermission();
        returnPermission.setSystemPermissions(list);

        //再查指定的接口
        List<String> destinate;
        if(roleId == 1){
         destinate = roleMapper.selectDestinate1(roleId);
        }else {
         destinate = roleMapper.selectDestinate2(roleId);
        }
        returnPermission.setAssignedPermissions(destinate);
        return returnPermission;
    }

    @Override
    public void updatePermission(RolePermission rolePermission) {
        List<String> permissions = rolePermission.getPermissions();
        //先删除所有的对应roleid的东西
        roleMapper.deletePermission(rolePermission.getRoleId());

        for (String permission : permissions) {
            roleMapper.updatePermission(permission,rolePermission.getRoleId());
        }
    }
}
